/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.front.plan;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.services.front.plan.PlanService;
import shop.services.front.plan.bean.Plan;
import shop.services.front.plancategory.PlancategoryService;
import shop.services.front.plancategory.bean.Plancategory;
import shop.services.front.product.ProductService;
import shop.services.front.product.bean.Product;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**   
 * @类名称：PlanController
 * @创建人：Wzl
 * @创建时间：2018-05-09 下午19:23:03      
 * @版本号：1.0
 * @描述：     
 */
@Controller
@RequestMapping("/front/plan")
public class PlanController extends FrontBaseController<Plan> {
    @Resource
    private PlanService planService;
    @Resource
    private PlancategoryService plancategoryService;
    @Resource
    private ProductService productService;
    @Override
    public Services<Plan> getService() {
        return planService;
    }

    /**
     * 查询全部方案列表
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectPlanList")
    @ResponseBody
    public JSONResult selectPlanList(Plancategory plancategory){
        List<Plancategory> categoryList = plancategoryService.selectList(plancategory);
        if(categoryList!=null&&categoryList.size()>0){
            for(Plancategory pc :categoryList){
                Plan plan=new Plan();
                plan.setCategoryID(pc.getId());
                List<Plan> planList = planService.selectList(plan);
                pc.setPlanList(planList);
            }
        }
        jsonResult = new JSONResult();
        jsonResult.setData(categoryList);
        return jsonResult;
    }

    /**
     * 查询方案的产品推荐列表
     *
     * @param plan
     * @return
     */
    @RequestMapping(value = "selectProductList", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectProductList(Plan plan) {
        plan = getService().selectOne(plan);
        String[] productID = plan.getProductID().split(",");
        List<Product> productList = new ArrayList<>();
        for (int i = 0; i < productID.length; i++) {
            Product product = new Product();
            product.setId(productID[i]);
            product = productService.selectOne(product);
            if (product != null){
                productList.add(product);
            }
        }
        jsonResult = new JSONResult();
        jsonResult.setData(productList);
        return jsonResult;
    }
}
